﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using VectorMath;
using log4net;
using SpaceFrame;

public class TwoDGeom
{
    private static readonly ILog log = LogManager.GetLogger(typeof(TwoDGeom));
    //constructor
	public TwoDGeom(PathFigure pf)
	{
        PathFigureCollection pfc = new PathFigureCollection();
        LineSegment ls = (LineSegment)pfc[1].Segments[1];
	}
    //Dec 10 2013
    //receives pairs of coordinates that define the starting and end points of straight line segments
    //these line segments are assumed to consist of coordinates that move in a counter-clockwise path to produce a
    //non-intersecting convex polygon
    public TwoDGeom(List<Vector.MemorySafe_CartCoord> coordlist)
    {
        log.Info("Coordlist initialized as TwoDGeom.");
        //does the list form a perfect enclosure?
        if (isPerfectlyEnclosed(coordlist))
        {
            log.Info("Surface passes all enclosure tests.");
        }
        //if yes, then create the perimeter zones and extrude
        log.Debug("Making Basic Room Template.");
        List<Vector.MemorySafe_CartCoord> intcds = Vector.ScalePolygonByPerimDepth(coordlist, 15, true);
        List<List<Vector.MemorySafe_CartCoord>> cds = new List<List<Vector.MemorySafe_CartCoord>>();
        cds.Add(coordlist);
        cds.Add(intcds);
        //make core
        log.Debug("Making core.");
        List<List<Vector.MemorySafe_CartCoord>> allcoords = Vector.FinishCore(cds, 0.3, true);
        log.Debug("Extrude points.");

        P2V p = new P2V();
        Project myp = new Project();
        myp = p.ExtrudeSimpleVertically(allcoords, 10);

        //get right hand rule
    }

    //Dec 10 2013
    //tests for a single surface
    //counterclockwise definition of vertices
    //non-self-intersecting
    private bool isPerfectlyEnclosed(List<Vector.MemorySafe_CartCoord> coordlist)
    {
        try
        {
            log.Info("Starting perfect enclosure tests.");
            //endpoints all match up
            if (coordlist.Count < 3)
            {
                log.Info("FAIL:  Too few points in coordlist for Enclosure Tests.");
                return false;
            }
            //is counterclockwise
            log.Info("##Starting polygon Counterclockwise Winding Test.##");
            var wd = Vector.isCounterClockwise(coordlist);
            if (wd == Vector.WalkDirection.Clockwise)
            {
                log.Info("PASS:  Coordinates are arranged in a counterclockwise fashion.");
            }
            else
            {
                log.Info("FAIL:  Coordinates appear to be in a clockwise direction.");
                return false;
            }
            //planarity test
            log.Info("##Starting polygon Planarity Test.##");
            if (Vector.isPlanar(coordlist))
            {
                log.Info("PASS:  Surface coordinates define a planar object.");
            }
            else
            {
                log.Info("FAIL:  Surface coordinates do not define a planar object.");
                return false;
            }
            //brute force self-intersection test
            log.Info("##Starting polygon Self-intersection Test.##");
            if (Vector.BruteForceIntersectionTest(coordlist))
            {
                log.Info("PASS:  Coordinates create a non-intersecting convex polygon.");
                log.Info("here is another one for testing.");
            }
            else
            {
                log.Info("FAIL:  Coordinates do not create a non-intersecting convex polygon.");
                return false;
            }

        }
        catch (Exception e)
        {
            log.Debug("An error was thrown in function IsPerfectlyEnclosed.");
            log.Error(e.ToString());
            return false;
        }
        return true;
    }

    //public methods
    //passes in a list of formulas or points that define a path, and retuns a list of discretized lines that define this path
    public bool GetandCheckFootprint()
    {
        return true;
    }

    public List<List<double>> DiscretizeLines(List<List<double>> floorPrintList)
    {
        List<List<double>> retList = new List<List<double>>();
        return retList;
    }

    public bool ScaleFootprint()
    {
        return true;
    }

    
    
}
